-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebGLRenderer: add reverse-z via EXT_clip_control #29445
Conversation
📦 Bundle sizeFull ESM build, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
This comment was marked as resolved.
This comment was marked as resolved.
This would only work with floating point depth buffers though - what about 24-bit depth buffers often used w/ stencil? |
No. See #29445 (comment) for instance, which uses the default canvas framebuffer. You lose near-field precision this way, but that's okay for mobile, where you want to keep this low(er) precision and avoid the blit. WebGPU gives more control here. We could enable a workaround where you set |
What I more mean is w/ 24-bit depth buffers is the precision uniform over [0, 1] - or am I wrong here? |
Precision is not uniform due to how perspective projection works and the useful range of floating point representation. We reverse this range to preserve far-field precision over near-field. In other words, the distribution does not change whether you use 32/24/16 bits for depth, but the possible range you represent in floating point. |
Related issue: #22017 (comment)
Description
Implements a reversed depth buffer with a [0, 1] projection matrix via
EXT_clip_control
, which is a strict improvement over logarithmic depth where supported. A reverse depth buffer exploits the high precision range of [0, ~0.8] and inverts depth to better the distribution at distance. This is not only significantly faster than logarithmic depth (where use ofgl_FragDepth
disables early-z optimizations and MSAA coverage) but more accurate. See the below articles, which visualize this effect.https://tomhultonharrop.com/mathematics/graphics/2023/08/06/reverse-z.html
https://developer.nvidia.com/blog/visualizing-depth-precision
As both a [0, 1] and reversed projection matrix would require drastic changes to sensitive code like frustum planes or raycasting, I've hidden both conversions when setting the
projectionMatrix
uniform inWebGLRenderer
. It's possible this can be moved to a shader chunk, but any inline clip-space math must account for this mode, and we'll need to add a shader define accordingly.The same is true for logarithmic depth, e.g., pmndrs/drei#1737. I've debated adding methods to packing chunks for these.
This example was ported from https://webgpu.github.io/webgpu-samples/?sample=reversedZ and uses a 32 bit depth-buffer.
https://jsfiddle.net/n69rqj8u
three-reverse-depth.mp4